<?php
/**
 * Created by PhpStorm.
 * User: xk_96
 * Date: 2018/1/15
 * Time: 11:11
 */

namespace system\drive;

class Db
{
    private $dbh = null;
    private static $instance;

    private function __construct()
    {
        $database = include BASE_ROOT . '/app/config/dataBase.php';
        try{
            $this->dbh = new \PDO('mysql:host=' . $database['host'] . ';dbname=' . $database['database'], $database['user'], $database['password']);
            $this->dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        }catch (PDOException $e){
            echo 'mysql error: ' . $e->getMessage();
        }

    }

    static public function getinstance()
    {
        if (!self::$instance) self::$instance = new self();
        return self::$instance;
    }

    /**
     * 获取多条记录
     * @param $sql
     * @return array
     * @author ybf
     */
    public static function query($sql)
    {
        $db = self::getinstance();
        $result = $db->dbh->query($sql);
        return $result->fetchAll();
    }

    /**
     * 获取一条记录
     * @param $sql
     * @return mixed
     * @author ybf
     */
     public static function get($sql){
         $db = self::getinstance();
         $result = $db->dbh->query($sql);
         return $result->fetch();
     }


     public static function exec($sql){
         $db = self::getinstance();
         $result = $db->dbh->exec($sql);
         return $result;
     }
}